package core;

import java.util.Hashtable;

import action.Action;
import action.Add;
import action.Delete;
import action.Edit;
import action.FindExec;
import action.GoogleCalendarAction;
import action.Postpone;
import action.Toggle;

import ui.View;
import data.Event;

public class UiController
{
	private Event[] result;
	/**
	 * @return the result
	 */

	private View view;
	private Hashtable<String, String> successMessages;
	private Thread resultBoxThread;
	private boolean newedit;
	String[][] commands = {{"add","added"},{"delete","deleted"}, {"edit","edited"},{"postpone","postponed"},
			{"undo","undid"}, {"login", "Logged in to Google Calendar"},
			{"update", "You are running the latest version :-)"}};

	public UiController() {
		Log.debug("UiCont: entering UiCont constructor");
		successMessages = new Hashtable<String, String>();
		for (int i = 0; i < commands.length; i++) {
			successMessages.put(commands[i][0], commands[i][1]);
		}
	}

	/**
	 * @return the view
	 */
	public View getView() {
		Log.debug("UiCont: entering getView");
		return view;
	}

	/**
	 * @param view the view to set
	 */
	public void setView(View view) {
		Log.debug("UiCont: entering setView");
		this.view = view;
	}

	public Event[] getResult() {
		Log.debug("UiCont: entering getResult");
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(Event[] result) {
		Log.debug("UiCont: entering setResult");
		this.result = result;
	}

	public void showResult(Action actionPerformer) {
		Log.debug("UiCont: showResult");
		if (resultBoxThread != null && resultBoxThread.isAlive()) {
			//resultBoxThread.interrupt();
		}

		if (result == null) {
			view.showOnlyBox();
			showResultBox(actionPerformer.errorMessage());
		} else if (actionPerformer instanceof Add || ((actionPerformer instanceof Edit || actionPerformer instanceof Postpone) && newedit==false) || (actionPerformer instanceof FindExec && ((FindExec)actionPerformer).isExecuting())) {
			String eventName = "";
			if (actionPerformer instanceof Add) {
				view.clearBoxText();
				eventName = result[0].getName();
			}

			if((actionPerformer instanceof Edit || actionPerformer instanceof Postpone) && newedit == true) {
				newedit = false;
			} else if (actionPerformer instanceof Delete){
				Event[] emptyArray= new Event[result.length];
				view.updateMultipleEventsInList(emptyArray);
			} else {
				eventName = "";
				if(result.length > 1) {
					for (int i = 0;i < result.length; i++) {
						eventName += result[i].getName()+", ";
					}
				} else {
					eventName = result[0].getName();
				}
				String resultString = String.format("%1$s %2$s successfully",eventName, successMessages.get(actionPerformer.getCommandName()));
				showResultBox(resultString);
				newedit = true;
			}

		} else if (actionPerformer instanceof GoogleCalendarAction) {
			String resultString = successMessages.get("login");
			showResultBox(resultString);			
		} else if (actionPerformer instanceof Toggle) {
			view.updateMultipleEventsInList(result);
		} else {
			if(result.length >= 1) {   
				view.showEventsInList(result);
			}
		}
	}

	private void showResultBox(String resultString) {
		Log.debug("UiCont: entering showResultBox");
		resultBoxThread = new Thread(view.displayResult(resultString));
		resultBoxThread.setDaemon(true);
		resultBoxThread.start();
	}
}